interface MessageOptions {
  duration?: number
  icon?: 'success' | 'error' | 'none'
  mask?: boolean
}

export const message = {
  // 普通提示
  info(title: string, options: MessageOptions = {}) {
    const { duration = 2000, mask = false } = options
    uni.showToast({
      title,
      icon: 'none',
      duration,
      mask,
    })
  },

  // 成功提示
  success(title: string, options: MessageOptions = {}) {
    const { duration = 2000, mask = false } = options
    uni.showToast({
      title,
      icon: 'success',
      duration,
      mask,
    })
  },

  // 错误提示
  error(title: string, options: MessageOptions = {}) {
    const { duration = 2000, mask = false } = options
    uni.showToast({
      title,
      icon: 'error',
      duration,
      mask,
    })
  },

  // loading提示
  loading(title: string = '加载中...', mask: boolean = true) {
    uni.showLoading({
      title,
      mask,
    })
  },

  // 隐藏loading
  hideLoading() {
    uni.hideLoading()
  },

  // 模态确认框
  confirm(content: string, options: UniApp.ShowModalOptions = {}) {
    return new Promise<boolean>((resolve) => {
      uni.showModal({
        content,
        showCancel: true,
        ...options,
        success: (res) => {
          resolve(res.confirm)
        },
      })
    })
  },

  // 模态提示框（无取消按钮）
  alert(content: string, options: UniApp.ShowModalOptions = {}) {
    return new Promise<void>((resolve) => {
      uni.showModal({
        content,
        showCancel: false,
        ...options,
        success: () => {
          resolve()
        },
      })
    })
  },
}
